#include "debuginfodialog.h"
#include "ui_debuginfodialog.h"

DebugInfoDialog::DebugInfoDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DebugInfoDialog),
    m_itemPerPage(25)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明
    setWindowModality(Qt::ApplicationModal);
    connect(g_pMachine, SIGNAL(siDebugInfo()), this, SLOT(slotDebugInfo()));

    initDialog();
    initControl();
}

DebugInfoDialog::~DebugInfoDialog()
{
    delete ui;
}
void DebugInfoDialog::initDialog()
{
    m_curPages = 1;//当前页数
    m_pageNums = 1;//总页数
    m_infoList.clear();
}
void DebugInfoDialog::initControl()
{
    m_DebugInfoBtnList.append(ui->button1);
    m_DebugInfoBtnList.append(ui->button2);
    m_DebugInfoBtnList.append(ui->button3);
    m_DebugInfoBtnList.append(ui->button4);
    m_DebugInfoBtnList.append(ui->button5);
    m_DebugInfoBtnList.append(ui->button6);
    m_DebugInfoBtnList.append(ui->button7);
    m_DebugInfoBtnList.append(ui->button8);
    m_DebugInfoBtnList.append(ui->button9);
    m_DebugInfoBtnList.append(ui->button10);
    m_DebugInfoBtnList.append(ui->button11);
    m_DebugInfoBtnList.append(ui->button12);
    m_DebugInfoBtnList.append(ui->button13);
    m_DebugInfoBtnList.append(ui->button14);
    m_DebugInfoBtnList.append(ui->button15);
    m_DebugInfoBtnList.append(ui->button16);
    m_DebugInfoBtnList.append(ui->button17);
    m_DebugInfoBtnList.append(ui->button18);
    m_DebugInfoBtnList.append(ui->button19);
    m_DebugInfoBtnList.append(ui->button20);
    m_DebugInfoBtnList.append(ui->button21);
    m_DebugInfoBtnList.append(ui->button22);
    m_DebugInfoBtnList.append(ui->button23);
    m_DebugInfoBtnList.append(ui->button24);
    m_DebugInfoBtnList.append(ui->button25);

    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
    //refreshUi();
}


//初始化窗体控件为任意分辨率
void DebugInfoDialog::initResolution()
{
    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

     m_itemPerPage = m_itemPerPage -8;
    this->resize(707*factoryX,513*factoryY);
    this->move(101*factoryX+g_mainWidgetPos.x(),36*factoryY+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0*factoryX,0*factoryY,710*factoryX,513*factoryY);
    ui->labelMainTitle->setGeometry(10*factoryX,6*factoryY,300*factoryX,28*factoryY);
    for(int i = 0; i < m_itemPerPage; i++)
    {
        m_DebugInfoBtnList[i]->setGeometry(10*factoryX,(30 + i * 27)*factoryY,500*factoryX,24*factoryY);
    }
    ui->labelPage->setGeometry(GLB_EDGE_WIGHT*factoryX,(GLB_SCR_HEIGHT-GLB_TEXT_HEIGHT-GLB_EDGE_WIGHT)*factoryY,(GLB_SCR_WIGHT/3)*factoryX,GLB_TEXT_HEIGHT*factoryY);
    ui->buttonPgUp->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*2+GLB_LBUT_WIGHT*3))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonPgDn->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*1+GLB_LBUT_WIGHT*2))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((GLB_SCR_WIGHT-(GLB_EDGE_WIGHT+GLB_BAS_WIGHT*0+GLB_LBUT_WIGHT*1))*factoryX,0*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
}

//初始化窗体控件样式
void DebugInfoDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());
    //背景图
    //QString style1 = "background-color: rgb(62, 62, 62);outline:none;border:0px;border-radius:" + borderRadius() + "px;";
    QString style1 = "background-color: rgb(246, 242, 239);outline:none;border:0px;border-radius:" + borderRadius() + "px;";
    ui->frameBack->setStyleSheet(style1);
    ui->labelMainTitle->setFont(fontSize_L());
    ui->labelMainTitle->setStyleSheet(titleTextColour());

    QString style2 = "border:0px;text-align:left;background-color:rgba(255, 255, 255, 0);color: rgb(78, 78, 78);";//0, 183, 13
    for(int i = 0; i < m_DebugInfoBtnList.size(); i++)
    {
        m_DebugInfoBtnList[i]->setStyleSheet(style2);//文本左对齐
        m_DebugInfoBtnList[i]->setFont(fontSize_M());
        m_DebugInfoBtnList[i]->setStyleSheet(commonTextColour());
    }
    ui->labelPage->setFont(fontSize_M());
    ui->labelPage->setStyleSheet(commonTextColour());
    ui->buttonPgUp->setStyleSheet(confirmIconStyle());
    ui->buttonPgUp->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgUp->objectName()),12);
    ui->buttonPgDn->setStyleSheet(confirmIconStyle());
    ui->buttonPgDn->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonPgDn->objectName()),12);
    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet(ui->buttonCancel->objectName()),12);
}
void DebugInfoDialog::addListInfo()
{
    m_infoList = g_pSettings->readToCsv(TYPE_DEBUGINFO);//读取   QStringList
    refreshUi();
}
void DebugInfoDialog::refreshUi()
{
    int itemNum;
    if (m_infoList.size() <= 0)
    {
        itemNum = 0;

        for (int i = 0; i < m_itemPerPage; i++)
        {
            m_DebugInfoBtnList.at(i)->hide();
        }
        ui->labelPage->setText(tr("pageNum: 0/0"));//页数: 0/0
        ui->buttonPgUp->setEnabled(false);
        ui->buttonPgDn->setEnabled(false);
        //ui->buttonOk->setEnabled(false);

        return;
    }
    else
    {
        itemNum = m_infoList.size();
    }

    m_pageNums = (itemNum + m_itemPerPage - 1)/m_itemPerPage;// 计算页数
    if (m_curPages > m_pageNums)// 当前页
    {
        m_curPages = m_pageNums;
    }
    if (m_curPages <= 1 && itemNum != 0)
    {
        m_curPages = 1;
    }

    int itemidx = (m_curPages - 1) * m_itemPerPage;

    for (int i = 0; i < m_itemPerPage; i++)
    {
        if (itemidx < itemNum)
        {
            //设置图标和文件夹名称
            m_DebugInfoBtnList[i]->setText(m_infoList.at(itemidx));
            m_DebugInfoBtnList.at(i)->show();

            itemidx++;
        }
        else
        {
            m_DebugInfoBtnList.at(i)->hide();
        }
    }

    // 翻页按钮
    if (m_curPages <= 1)
    {
        ui->buttonPgUp->setEnabled(false);
    }
    else
    {
        ui->buttonPgUp->setEnabled(true);
    }
    if (m_curPages >= m_pageNums)
    {
        ui->buttonPgDn->setEnabled(false);
    }
    else
    {
        ui->buttonPgDn->setEnabled(true);
    }

    // 页信息
    if(m_pageNums == 0)
    {
        m_curPages = 0;
    }

    QString str = tr("pageNum: ") + QString("%1/%2").arg(m_curPages).arg(m_pageNums);//页数:
    ui->labelPage->setText(str);
}
void DebugInfoDialog::slotDebugInfo()
{
    DebugInfo info;//调试信息
    char mDebugInfoStr[1024+1];

    info = g_pMachine->getDebugInfo();
    memcpy(mDebugInfoStr, info.debugInfoStr, 1024);
    mDebugInfoStr[1024] = 0;

    QString strDebugInfoStr;
    strDebugInfoStr.sprintf("%s", mDebugInfoStr);
    QString strCurTime = QDateTime::currentDateTime().toString("yyyy/MM/dd/hh:mm:ss");//时间
    QString strMessage;
    strMessage = QString("%1//%2").arg(strCurTime).arg(strDebugInfoStr);//时间，内容
    m_infoList.insert(0,strMessage);

    //m_infoList = g_pSettings->readToCsv(TYPE_DEBUGINFO);//读取   QStringList
    refreshUi();
}
void DebugInfoDialog::on_buttonCancel_clicked()
{
     done(0);
}
void DebugInfoDialog::on_buttonPgUp_clicked()
{
    m_curPages--;
    refreshUi();
}
void DebugInfoDialog::on_buttonPgDn_clicked()
{
    m_curPages++;
    refreshUi();
}
